package com.huiyujiaoyu.powertime.ui.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.SimpleBean;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.AppManager;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.NumberUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.LlkjToolBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.huiyujiaoyu.util.NumberUtil.toDouble;

/**
 * Created by win7 on 2017/6/18.
 * 我的收藏
 */

public class OpenVipActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.tv_youhuiquan)
    TextView mTvYouhuiquan;
    @BindView(R.id.tv_paymoney)
    TextView mTvPaymoney;
    @BindView(R.id.btn_open)
    Button btn_open;
    @BindView(R.id.iv_header)
    ImageView iv_header;
    private String yhid = "0",yhmoney;
    private boolean isOK;
    public static void launch(Context context) {
        ActivityUtil.startActivity(context, OpenVipActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        setToolbar(mToolbar);
        mToolbar.setToolBar("开通VIP", true, "", R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
        GlideUtil.loadImg(iv_header, StringUtils.getFullUrl(UserInfoBean.getInstance().getHeadImg()));
        HttpMethods.getInstance().coupons(mContext, getComp(), this, 1,1);
        isOK = NumberUtil.toDouble(UserInfoBean.getInstance().getBalance()).compareTo(NumberUtil.toDouble(366)) ==-1;
        if(isOK){
            btn_open.setText("余额不足，请充值");
        }else{
            btn_open.setText("立即开通");
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_openvip;
    }


    @OnClick({R.id.btn_open,R.id.tv_youhuiquan})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_open:
                if(isOK){

                }else{
                    HttpMethods.getInstance().buycolumn(mContext,getComp(),this,yhid,2);
                }
                break;
            case R.id.tv_youhuiquan:
                Bundle b = new Bundle();
                b.putInt(Constant.DATA,1);
                ActivityUtil.startActivityForResult(this,MyYHQActivity.class,b,100);
                break;
        }
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_COUPONS:
                SimpleListBean bean = (SimpleListBean) o;
                int size = bean.unused.size();
                mTvYouhuiquan.setText(size>0?size+"张优惠券可用":"无优惠券可用");
                break;
            case HttpApi.HTTP_BUYCOLUMN:
                SimpleBean sb = (SimpleBean) o;
                UserInfoBean.getInstance().setIsVip("1");
                UserInfoBean.getInstance().setExpiry_date(sb.endtime);
                UserInfoBean.getInstance().setBalance(sb.balance);
                AppManager.getAppManager().finishActivity(MyVipActivity.class);
                finish();
                MyVip2Activity.launch(mContext);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode) {
                case 100:
                    yhid = data.getStringExtra(Constant.DATA);
                    yhmoney = data.getStringExtra(Constant.DATA2);
                    mTvYouhuiquan.setText("可优惠"+yhmoney+"元");
                    break;
            }
        }
    }
}
